package com.tian.aop.faskclick

import android.util.Log
import android.view.View
import com.tian.aop.tagFastClick
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.reflect.MethodSignature

/**
 * view 快速点击的切面类
 */
@Aspect
class FastClickLimitAspect {

    @Pointcut("execution(@com.tian.aop.faskclick.FastClickLimit * *(..))")
    fun executeFastClickViewLimit() {
    }

    @Around("executeFastClickViewLimit()")
    @Throws(Throwable::class)
    fun aroundExecuteFastClickViewLimit(joinPoint: ProceedingJoinPoint) {
        Log.d(tagFastClick, "aroundClickCountLimit: ")
        val signature: MethodSignature = joinPoint.signature as MethodSignature
        // 取出JoinPoint的方法
        val method = signature.method
        if (method.isAnnotationPresent(FastClickLimit::class.java)) {
            val annotation: FastClickLimit? = method.getAnnotation(FastClickLimit::class.java)
            annotation?.let {
                val interval = annotation.interval
                val view = joinPoint.args[0] as View
                if (!FastClickCheckUtil.isFastClick(view, interval)) {
                    joinPoint.proceed()
                }
            }
        }
    }

}